package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.search.pojo.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    @Autowired
    private ApArticleContentMapper contentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 通过文字的id生成静态的页面
     *
     * @param articleId
     */
    @Override
    @Async
    public void buildArticleToMinIO(Long articleId) throws IOException, TemplateException {
        //1. 根据文章的id查询文章的内容

        ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, articleId));

        //2. 获取freemarker的模板对象并生成一个静态的html
        Template template = configuration.getTemplate("article.ftl");
        StringWriter out = new StringWriter();

        Map<String, Object> params = new HashMap<>();
        params.put("content", JSONArray.parseArray(articleContent.getContent()));

        template.process(params, out);

        //3. 把html上传到minio中并返回访问的地址
        InputStream input = new ByteArrayInputStream(out.toString().getBytes());
        String path = fileStorageService.uploadHtmlFile("", articleContent.getArticleId() + ".html", input);

        //4. 把访问的地址更新到ap_article表中

        ApArticle entity = apArticleMapper.selectById(articleContent.getArticleId());
        entity.setStaticUrl(path);
        apArticleMapper.updateById(entity);

        //5. 发送消息到MQ
        createArticleData(entity, articleContent.getContent(), path);
    }

    private void createArticleData(ApArticle entity, String content, String path) {
        SearchArticleVo vo = new SearchArticleVo();
        BeanUtils.copyProperties(entity, vo);
        vo.setContent(content);
        vo.setStaticUrl(path);
        vo.setId(entity.getId()+"");

        //发送消息
        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));
    }
}
